<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('collectionBusiness.tabData.state')">
          <el-select v-model="searchForm.businessConfigState" clearable :placeholder="$t('collectionBusiness.prompt.state')">
            <el-option
              v-for="item in $t('collectionBusiness.state')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-row>
        <el-button type="primary" @click="add">
          {{ $t('collectionBusiness.add') }}
        </el-button>
      </el-row>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%"
      >
        <el-table-column
          fixed="left"
          prop="id"
          :label="tableText['id']"
          min-width="100"
        />
        <el-table-column
          fixed="left"
          prop="phaseName"
          :label="tableText['name']"
          min-width="150"
        />
        <el-table-column
          prop="startOverdueTime"
          :label="tableText['fromTime']"
          min-width="150"
        />
        <el-table-column
          prop="endOverdueTime"
          :label="tableText['toTime']"
          min-width="150"
        />
        <el-table-column
          prop="retainedDays"
          :label="tableText['days']"
          min-width="170"
        />
        <el-table-column
          prop="flowPattern"
          :label="tableText['flowMode']"
          min-width="200"
        >
          <template slot-scope="scope">
            {{ setValue(scope.row['flowPattern'], 'flowMode') }}
          </template>
        </el-table-column>
        <el-table-column
          prop="loanTermWithoutReminder"
          :label="tableText['freeLoanTerm']"
          min-width="150"
        />
        <el-table-column
          fixed="right"
          :label="tableText['state']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ setValue(scope.row['state'], 'state') }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          :label="$t('common.operate.name')"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-button size="small" @click="edit(scope.row)">{{ $t('common.operate.edit') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <business-config-dialog :show-dialog="editDialog" :choose-item="chooseItem" @change-dialog="changeDialog" @edit-success="editSuccess" />
  </el-row>
</template>

<script>
import { getBusinessConfig } from '@/api/collection'
import BusinessConfigDialog from '@/components/collection/business-config-dialog.vue'
export default {
  name: 'CollectionBusiness',
  components: {
    BusinessConfigDialog
  },
  data() {
    return {
      loading: true,
      tableData: [],
      tableText: {},
      totalNumber: 0,
      editDialog: false,
      freeModel: false,
      dayModel: false,
      searchForm: this.params(),
      chooseItem: {
        id: null,
        phaseName: null,
        startOverdueTime: null,
        endOverdueTime: null,
        flowPattern: null,
        retainedDays: null,
        loanTermWithoutReminder: null,
        select: null,
        state: null,
        dayModel: null,
        freeModel: null
      }
    }
  },
  mounted() {
    this.tableText = this.$t('collectionBusiness.tabData')
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        businessConfigState: null
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.loading = true
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.getData()
    },
    getData() {
      this.loading = true
      getBusinessConfig(this.searchForm)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    setValue(value, data) {
      const map = this.$t(`collectionBusiness.${data}`)
      const name = map.filter(item => {
        return item.value === value
      })
      return (name.length > 0 && name[0].desc) ? name[0].desc : ''
    },
    edit(item) {
      this.choose(item)
      this.editDialog = true
    },
    add() {
      this.choose({})
      this.editDialog = true
    },
    choose(param) {
      this.chooseItem.id = param.id
      this.chooseItem.phaseName = param.phaseName
      this.chooseItem.startOverdueTime = param.startOverdueTime
      this.chooseItem.endOverdueTime = param.endOverdueTime
      this.chooseItem.flowPattern = param.flowPattern
      this.chooseItem.retainedDays = param.retainedDays
      this.chooseItem.state = param.state
      this.chooseItem.dayModel = !!(param.retainedDays)
      if (param.loanTermWithoutReminder) {
        const x = this.getNum(param.loanTermWithoutReminder)
        this.chooseItem.freeModel = x > 0
        this.chooseItem.loanTermWithoutReminder = x > 0 ? param.loanTermWithoutReminder.substring(x, -1) : null
        this.chooseItem.select = x > 0 ? param.loanTermWithoutReminder.substring(0, x) : null
      } else {
        this.chooseItem.freeModel = false
      }
    },
    changeDialog(param) {
      this.editDialog = param
      setTimeout(() => {
        this.choose({})
      }, 10)
    },
    editSuccess() {
      this.getData()
    },
    getNum(str) {
      if (str) {
        const pattern = new RegExp('[0-9]+')
        if (str.indexOf('-') > -1) {
          return str.indexOf('-')
        } else {
          const num = str.match(pattern)
          return str.indexOf(num)
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
.el-table{
  margin-top: 10px;
}
</style>
